<?php

namespace App\Http\Controllers\Admin;

use App\Handlers\Tree;
use App\Http\Requests\Admin\AdminRequest;
use App\Repositories\RulesRepository;
use App\Services\Config\ChannelsService;
use Illuminate\Http\Request;
use App\Services\AdminsService;
use App\Repositories\RolesRepository;
use App\Http\Requests\Admin\AdminLoginRequest;
use Cache;

class AdminsController extends BaseController
{
    protected $adminsService;

    protected $rolesRepository;

    /**
     * AdminsController constructor.
     * @param AdminsService $adminsService
     * @param RolesRepository $rolesRepository
     */
    public function __construct(AdminsService $adminsService, RolesRepository $rolesRepository, ChannelsService $channelsService)
    {
        $this->adminsService = $adminsService;

        $this->rolesRepository = $rolesRepository;
        $this->channelsService = $channelsService;
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $reqPageSize = $request->page_size;
        $admins = $this->adminsService->getAdmins($request);

        return $this->view(null, compact('admins', 'reqPageSize'));
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create(RulesRepository $rulesRepository, Tree $tree)
    {
        $defaultCheck = false;

        $rules = $rulesRepository->getRules();
        $datas = $tree::channelLevel($rules, 0, '&nbsp;', 'id', 'parent_id');

        return view('admin.admins.create', compact('defaultCheck', 'datas'));
    }

    /**
     * @param AdminRequest $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(AdminRequest $request)
    {
        $this->adminsService->create($request);

        flash('添加管理员成功')->success()->important();

        return $this->redirect($request);
    }


    /**
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id, RulesRepository $rulesRepository, Tree $tree)
    {
        $admin = $this->adminsService->ById($id);
        $reqGameId = $admin->games()->get()->toArray();
        $reqGameId = array_column($reqGameId, 'game_id');
        $reqChannelTableId = $admin->channels()->get()->toArray();
        $reqChannelTableId = array_column($reqChannelTableId, 'id');
        $reqRuleId = $admin->rules()->get()->toArray();
        $reqRuleId = array_column($reqRuleId, 'id');

        $rules = $rulesRepository->getRules();
        $datas = $tree::channelLevel($rules, 0, '&nbsp;', 'id', 'parent_id');

        return view('admin.admins.edit', compact('admin', 'reqGameId', 'reqChannelTableId', 'datas', 'reqRuleId'));
    }

    /**
     * @param AdminRequest $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(AdminRequest $request, $id)
    {
        $this->adminsService->update($request, $id);

        Cache::tags('rbac')->flush();

        flash('更新资料成功')->success()->important();

        return $this->redirect($request);
    }

    /**
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function delete($id)
    {
        $admin = $this->adminsService->ById($id);

        if (empty($admin)) {
            flash('删除失败')->error()->important();

            return redirect()->route('admins.index');
        }


        //$admin->roles()->detach();

        $admin->delete();


        flash('删除成功')->success()->important();

        return redirect()->route('admins.index');
    }

    /**
     * @param $status
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function status($status, $id)
    {
        $admin = $this->adminsService->ById($id);

        if (empty($admin)) {
            flash('操作失败')->error()->important();

            return redirect()->back();
        }

        $admin->update(['status' => $status]);

        flash('更新状态成功')->success()->important();

        return redirect()->back();
    }

    public function showLoginForm()
    {
        return view('admin.admins.login');
    }

    /**
     * 管理员登陆
     * @param AdminLoginRequest $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function loginHandle(AdminLoginRequest $request)
    {
        $result = $this->adminsService->login($request);

        if (!$result) {
            return viewError('登录失败', 'login');
        }

        return viewError('登录成功!', 'index.index', 'success', null, 1);
    }

    /**
     * 退出登录
     * @return \Illuminate\Http\RedirectResponse
     */
    public function logout()
    {
        $this->adminsService->logout();

        return redirect()->route('login');
    }
}
